package test06.tx2;

import java.util.LinkedList;

// 篮子 - 装水果
public class Box {
    // 篮子容器
    private LinkedList<Fruit> list = new LinkedList();
    // 定义常量 - 表达篮子最大的容量
    private static final int MAX_SIZE = 5;
    // 男孩放水果
    public synchronized void put(String name, Fruit f) {
        while (list.size() == MAX_SIZE) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        list.add(f);
        System.out.printf("当前篮子的容量：[%d]，%s放%s进篮子%n",MAX_SIZE-list.size(),name,f);
        this.notifyAll();

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
    // 女孩取水果
    public synchronized void take(String name) {
        while (list.isEmpty()){
            try {
                // 女孩停止吃水果
                this.wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return;
        }
        Fruit doelfruit = list.removeLast();
        System.out.printf("当前篮子的容量：[%d]，%s吃篮子里的%s%n",MAX_SIZE-list.size(),name,doelfruit);
        this.notifyAll();

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}
